package com.lin.sqltool.controller;

import com.lin.sqltool.common.BaseQueryController;
import com.lin.sqltool.common.SqlExecutionResult;
import com.lin.sqltool.factory.ConnectionServiceFactory;
import com.lin.sqltool.model.ConnectionInfoModel;
import com.lin.sqltool.service.ConnectionService;
import com.lin.sqltool.utils.CommonUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.List;

import static com.lin.sqltool.utils.CommonUtil.showAlert;

public class CommonQueryController implements BaseQueryController {

    @FXML
    private ComboBox<String> databaseCombo;

    @FXML
    private TextArea sqlEditor;

    @FXML
    private TableView<List<String>> resultTable;

    @FXML
    private TextArea resultTextArea;


    private ConnectionInfoModel connectionInfo;

    private ConnectionService connectionService;


    /**
     * 设置连接信息
     * @param connectionInfo
     */
    public void setConnectionInfo(ConnectionInfoModel connectionInfo) {
        this.connectionInfo = connectionInfo;
        this.connectionService = ConnectionServiceFactory.getService(connectionInfo.getDatabaseType());
        this.databaseCombo.getItems().addAll(connectionService.getDatabases(connectionInfo));
    }

    /**
     * 设置数据库
     * @param database
     */
    public void setDatabaseInfo(String database) {
        this.databaseCombo.setValue(database);
    }


    /**
     * 执行选中内容
     */
    @FXML
    private void handleRunQuery() {
        String selectedDatabase = databaseCombo.getValue();
        String sql = sqlEditor.getSelectedText();
        if (selectedDatabase == null) {
            showAlert(Alert.AlertType.ERROR, "失败", "请先选择数据库");
        }
        SqlExecutionResult result = connectionService.executeSql(connectionInfo, selectedDatabase, sql);
        resultTable.setVisible(false);
        resultTextArea.setVisible(false);

        if (result.isQuery()) {
            // 显示表格
            resultTable.setVisible(true);
            resultTable.getColumns().clear();
            List<List<String>> tableData = result.getTableData();
            if (!tableData.isEmpty()) {
                CommonUtil.setTableData(resultTable, tableData);
            }
        } else {
            // 显示文本域
            resultTextArea.setVisible(true);
            resultTextArea.setText(result.getMessage());
        }
    }

    /**
     * 格式化选中内容
     * @param actionEvent
     */
    public void handleFormat(ActionEvent actionEvent) {
        String sql = sqlEditor.getSelectedText();
        sql = sql.replace("from", "\nfrom");
        sql = sql.replace("where", "\nwhere");
        sql = sql.replace("and", "\nand");
        sql = sql.replace("limit", "\nlimit");
        sql = sql.replace("group", "\ngroup");

        sql = sql.replace("FROM", "\nFROM");
        sql = sql.replace("WHERE", "\nWHERE");
        sql = sql.replace("AND", "\nAND");
        sql = sql.replace("LIMIT", "\nLIMIT");
        sql = sql.replace("GROUP", "\nGROUP");
        sqlEditor.replaceSelection(sql);
    }

    /**
     * 开放给连接使用使用
     * @param sql
     */
    public void runSQL(String sql) {
        String selectedDatabase = databaseCombo.getValue();
        if (selectedDatabase == null) {
            showAlert(Alert.AlertType.ERROR, "失败", "请先选择数据库");
        }
        SqlExecutionResult result = connectionService.executeSql(connectionInfo, selectedDatabase, sql);
        resultTable.setVisible(false);
        resultTextArea.setVisible(false);

        if (result.isQuery()) {
            // 显示表格
            resultTable.setVisible(true);
            resultTable.getColumns().clear();
            List<List<String>> tableData = result.getTableData();
            if (!tableData.isEmpty()) {
                CommonUtil.setTableData(resultTable, tableData);
            }
        } else {
            // 显示文本域
            resultTextArea.setVisible(true);
            resultTextArea.setText(result.getMessage());
        }
    }
}
